<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="文章名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入文章名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"

        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="classifyList" @selection-change="handleSelectionChange">
      <el-table-column label="分类编号" prop="classifyId" width="150" />
      <el-table-column label="名称" prop="name" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="文章类型" prop="type" width="150" />
      <el-table-column label="排序" prop="orderNum" width="150" />
      <el-table-column label="状态" prop="status" width="150" />

      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.clssifyId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-comment"
            @click="handleGetInfo(scope.row)"

          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入类型名称" maxlength="30" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item label="文章类型" prop="type">
            <el-input v-model="form.type" placeholder="请输入类型名称" />
          </el-form-item>

          <el-form-item label="排序" prop="orderNum">
            <el-input v-model="form.orderNum" placeholder="请输入排序" />
          </el-form-item>

          <el-form-item label="状态" prop="status">
            <el-input v-model="form.status" placeholder="请输入文章状态" />
          </el-form-item>


        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import { treeselect as menuTreeselect, roleMenuTreeselect } from "@/api/system/menu";
import {
  addClassify,
  delClassify,
  getClassify,
  listClassify,
  updatClassify,
  updateClassify
} from "@/api/article/classify";
import article from "@/views/article/classify/index0";
import {addUser, getUser, updateUr} from "@/api/system/user";
import {getArticle} from "@/api/article/article";

export default {
  name: "classify",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      classifyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleName: undefined,
        roleKey: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        author: [
          { required: true, message: "作者不能为空", trigger: "blur" }
        ],
        views: [
          { required: true, message: "销量不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "库存不能为空", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "顺序不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listClassify(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.classifyList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 查询菜单树结构 */
    getMenuTreeselect() {
      menuTreeselect().then(response => {
        this.menuOptions = response.data;
      });
    },
    /** 根据角色ID查询菜单树结构 */
    getRoleMenuTreeselect(roleId) {
      return roleMenuTreeselect(roleId).then(response => {
        this.menuOptions = response.menus;
        return response;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
      this.menuExpand = false,
        this.menuNodeAll = false,
        this.deptExpand = true,
        this.deptNodeAll = false,
        this.form = {
          classifyId: undefined,
          name: undefined,
          type: undefined,
          description: 0,
          orderNum: undefined,
          views: undefined,
          roleSort: 0,
          status: "0",
          menuIds: [],
          deptIds: [],
          menuCheckStrictly: true,
          deptCheckStrictly: true,
          remark: undefined
        };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getMenuTreeselect();
      this.open = true;
      this.title = "添加文章";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const classifyId = row.classifyId || this.ids;
      getClassify(classifyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文章";
        // this.form.password = "";
      });
    },
    handleGetInfo(row) {
      //this.$router.push({ path: "/article/edit", query: { articleId: row.articleId } });
      this.reset();
      // this.$router.push({ path: "/article/edit"  });
      const classifyId = row.classifyId || this.ids;
      getClassify(classifyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "文章详情";
        // this.form.password = "";
      });
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.classifyId != undefined) {
            updateClassify(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }else {
            addClassify(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }

        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const classifyIds = row.classifyId || this.ids;
      this.$modal.confirm('是否确认删除排序编号为"' + classifyIds + '"的数据项？').then(function() {
        return delClassify(classifyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('article/classify/export', {
        ...this.queryParams
      }, `classify_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
